package fx.com;

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.Mnemonic;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Main extends Application {
    ImageView imageView;
    Stage primary, stage;
    double SceneX_start, SceneY_start, SceneX_end, SceneY_end;

    HBox box;


    @Override
    public void start(Stage primaryStage) throws Exception {

        primary = primaryStage;
        Button button = new Button("??????");

        imageView = new ImageView();
        imageView.setFitWidth(700);
        imageView.setPreserveRatio(true);


        AnchorPane an = new AnchorPane();
        an.getChildren().addAll(button, imageView);
        AnchorPane.setTopAnchor(button, 50.0);
        AnchorPane.setLeftAnchor(button, 50.0);

        AnchorPane.setTopAnchor(imageView, 100.0);
        AnchorPane.setLeftAnchor(imageView, 50.0);


        Scene scene = new Scene(an);
        primaryStage.setScene(scene);
        primaryStage.setTitle("???????");
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);
        primaryStage.show();
        button.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                show();
            }
        });
        //????
        KeyCombination key = KeyCombination.valueOf("ctrl+alt+p");
        Mnemonic mc = new Mnemonic(button, key);
        scene.addMnemonic(mc);
    }

    public static void main(String[] args) {
        launch(args);
    }

    /**
     * ???????????????????
     */
    public void show() {
        //???????????????
        primary.setIconified(true);


        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color:#B5B5B522");
        Scene scene = new Scene(an);
        stage = new Stage();
        //?????????????????????????
        stage.initStyle(StageStyle.TRANSPARENT);
        //????????????
        scene.setFill(Paint.valueOf("#ffffff00"));
        //????????
        stage.setFullScreenExitHint("");
        stage.setScene(scene);
        //???????
        stage.setFullScreen(true);
        stage.show();

        drag(an);
        scene.setOnKeyPressed(new EventHandler<KeyEvent>() {

            @Override
            public void handle(KeyEvent event) {
                if (event.getCode() == KeyCode.ESCAPE) {
                    //?????????
                    stage.close();
                    //?????????
                    primary.setIconified(false);
                }

            }
        });
    }

    /**
     * ??????????
     */
    public void drag(AnchorPane an) {
        an.setOnMousePressed(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                //???
                an.getChildren().clear();
                //??????
                box = new HBox();
                box.setBackground(null);
                box.setBorder(new Border(new BorderStroke(Paint.valueOf("#cd3700"), BorderStrokeStyle.SOLID, null, new BorderWidths(2))));

                SceneX_start = event.getSceneX();
                SceneY_start = event.getSceneY();
                an.getChildren().add(box);

                AnchorPane.setTopAnchor(box, SceneY_start);
                AnchorPane.setLeftAnchor(box, SceneX_start);
            }
        });
        //??????
        an.setOnDragDetected(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                // TODO Auto-generated method stub
                an.startFullDrag();
            }
        });
        //??????????
        an.setOnMouseDragOver(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                Label label = new Label();
                label.setAlignment(Pos.CENTER);
                label.setPrefWidth(160);
                label.setPrefHeight(30);
                label.setTextFill(Paint.valueOf("#ffffff"));
                label.setStyle("-fx-background-color:#000000");
                AnchorPane.setTopAnchor(label, SceneY_start - label.getPrefHeight());
                AnchorPane.setLeftAnchor(label, SceneX_start);
                an.getChildren().add(label);
                double scenX = event.getSceneX();
                double scenY = event.getSceneY();

                double width = scenX - SceneX_start;
                double height = scenY - SceneY_start;
                box.setPrefWidth(width);
                box.setPrefHeight(height);
                label.setText("???" + height + "???" + width);
            }
        });
        //???
        an.setOnMouseDragExited(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                // TODO Auto-generated method stub
                SceneX_end = event.getSceneX();
                SceneY_end = event.getSceneY();
                Button button = new Button("???");

                box.getChildren().add(button);
                box.setAlignment(Pos.BOTTOM_RIGHT);
                button.setOnAction(new EventHandler<ActionEvent>() {

                    @Override
                    public void handle(ActionEvent arg0) {
                        getScreeImg();

                    }
                });
            }
        });
    }

    public void getScreeImg() {
        //?????????????????
        stage.close();
        double w = SceneX_end - SceneX_start;
        double h = SceneY_end - SceneY_start;

        //???awt
        Robot robot;
        try {
            robot = new Robot();
            //????
            Rectangle scr = new Rectangle((int) SceneX_start, (int) SceneY_start, (int) w, (int) h);
            BufferedImage bufImage = robot.createScreenCapture(scr);
            //awt?fx
            WritableImage image = SwingFXUtils.toFXImage(bufImage, null);

            imageView.setImage(image);


            //?????????
            Clipboard cb = Clipboard.getSystemClipboard();//????????????
            ClipboardContent clipboardContent = new ClipboardContent();
            clipboardContent.putImage(image);
            cb.setContent(clipboardContent);

            //???????
            ImageIO.write(bufImage, "jpeg", new File("D:/img.jpg"));
            primary.setIconified(false);
        } catch (Exception e) {

            e.printStackTrace();
        }

    }

}
